home *** CD-ROM | disk | FTP | other *** search
Wrap
function deviantAnywhere() { this.messagesURL = "http://my.deviantart.com/messages/"; this.loginURL = "https://www.deviantart.com/users/login"; this.REGEXP_DEVIATIONS = /<a href=".*?">(\d+) (?:Deviation|Deviations)<\/a>/; //<a href=".*?">(\d+) (?:Deviation|Deviations)</a> this.REGEXP_MESSAGES = /<a href="http:\/\/my\.deviantart\.com\/messages\/"(?: title="(.*?)")?>(.*?)<\/a>/; //<a href="http://my\.deviantart\.com/messages/"(?: title="(.*?)")?>(.*?)</a> this.REGEXP_NOTES = /<a href=".*?">(\d+) (?:Note|Notes)<\/a>/; //<a href=".*?">(\d+) (?:Note|Notes)</a> this.REGEXP_CONTESTS = /\["oq:contests.*?"count":(\d)+/; //\["oq:contests.*?"count":(\d)+ this.REGEXP_ADMIN = /\["oq:admin.*?"count":(\d+)/; //\["oq:admin.*?"count":(\d+) this.REGEXP_SPLIT_MSG = /(\d+)(\w{1,2})/ //(\d+)(\w{1,2}) this.REGEXP_DEVIANTNAME = /<div class="text loggedin">(?:<span>Deviant <\/span>)| .*?<span class="loggedin a">(.)<a class="u" href=".*?">(.*?)<\/a>/ //<div class="text loggedin">(?:<span>Deviant </span>)| .*?<span class="loggedin a">(.)<a class="u" href=".*?">(.*?)</a> this.REGEXP_CRITIQUES = /\["oq:devwatch:\d*?:\d*?:f:tg=critiques"\].*?"count":(\d*)/ //\["oq:devwatch:\d*?:\d*?:f:tg=critiques"\].*?"count":(\d*) this.REGEXP_PREV_DEVIATIONS = /"display_class":\["deviation".*?"username":".*?","who":"(.)<a class=\\"u\\" href=\\"(.*?)">(.*?)<\\\/a>",.*?"title":"<a href=\\"(.*?)">(.*?)<\\\/a>",/ //"display_class":\["deviation".*?"username":".*?","who":"(.)<a class=\\"u\\" href=\\"(.*?)">(.*?)<\\/a>",.*?"title":"<a href=\\"(.*?)">(.*?)<\\/a>", this.REGEXP_PREV_NOTES = /"app":"unread notes","who":"(.)<a class=\\"u\\" href=\\"(.*?)">(.*?)<\\\/a>".*?"title":"<a href=\\"(.*?)">(.*?)<\\\/a>"/ //"app":"unread notes","who":"(.)<a class=\\"u\\" href=\\"(.*?)">(.*?)<\\/a>".*?"title":"<a href=\\"(.*?)">(.*?)<\\/a>" this.REGEXP_PREV_FAVS = /\["collect","activity","line"\],"who":"(.)<a class=\\"u\\" href=\\"(.*?)\">(.*?)<\\\/a>.*?"collection":"<a href=\\"(.*?)\">(.*?)<\\\/a>.*?"title":"<a href=\\"(.*?)\\">(.*?)<\\\/a>/ //\["collect","activity","line"\],"who":"(.)<a class=\\"u\\" href=\\"(.*?)\">(.*?)<\\/a>.*?"collection":"<a href=\\"(.*?)\">(.*?)<\\/a>.*?"title":"<a href=\\"(.*?)\\">(.*?)<\\/a> this.REGEXP_PREV_WATCH = /\["watched","activity","line"\],"who":"(.)<a class=\\"u\\" href=\\"(.*?)\\">(.*?)<\\\/a>/ //\["watched","activity","line"\],"who":"(.)<a class=\\"u\\" href=\\"(.*?)\\">(.*?)<\\/a> this.REGEXP_PREV_COMMENTS = /\["comment","text"\].*?"who":"(.)<a class=\\"u\\" href=\\"(.*?)\\">(.*?)<\\\/a>.*?"title":".??<a.*?href=\\"(.*?)\\">(.*?)<\\\/a>.*?"url":"(.*?)"/ //\["comment","text"\].*?"who":"(.)<a class=\\"u\\" href=\\"(.*?)\\">(.*?)<\\/a>.*?"title":".??<a.*?href=\\"(.*?)\\">(.*?)<\\/a>.*?"url":"(.*?)" this.REGEXP_PREV_CRITIQUES = /"app":"deviantWATCH","ts":"\d*?","display_class":\["critique","text"\],"line":"(.)<a .*?href=\\"(.*?)\\\/\\">(.*?)<\\\/a> posted a <a.*?href=\\"(.*?)\">critique<\\\/a> of <i class=\\"icon i2\\"><\\\/i> <a.*?href=\\"(.*?)\\">(.*?)<\\\/a> by (.)<a.*?href=\\"(.*?)\\\/\\">(.*?)<\\\/a>/ //"app":"deviantWATCH","ts":"\d*?","display_class":\["critique","text"\],"line":"(.)<a .*?href=\\"(.*?)\\/\\">(.*?)<\\/a> posted a <a.*?href=\\"(.*?)\">critique<\\/a> of <i class=\\"icon i2\\"><\\/i> <a.*?href=\\"(.*?)\\">(.*?)<\\/a> by (.)<a.*?href=\\"(.*?)\\/\\">(.*?)<\\/a> this.REGEXP_PREV_JOURNALS = /"app":"deviantWATCH","display_class":\["journal","text"\].*?"username":".*?","who":"(.)<a class=\\"u\\" href=\\"(.*?)">(.*?)<\\\/a>",.*?"title":"<a href=\\"(.*?)">(.*?)<\\\/a>",/ //"app":"deviantWATCH","display_class":\["journal","text"\].*?"username":".*?","who":"(.)<a class=\\"u\\" href=\\"(.*?)">(.*?)<\\/a>",.*?"title":"<a href=\\"(.*?)">(.*?)<\\/a>", this.descs = new Array(); this.descs['N'] = "Notice::Notices"; this.descs['A'] = "Activity Message::Activity Messages"; this.descs['R'] = "Reply::Replies"; this.descs['UN'] = "Unread Note::Unread Notes"; this.descs['J'] = 'Journal::Journals'; this.descs['C'] = 'Comment::Comments'; this.descs['D'] = 'Deviation::Deviations'; this.descs['P'] = 'Poll::Polls'; this.descs['CA'] = 'Contest Announcement::Contest Announcements'; this.descs['NW'] = 'News Article::News Articles'; this.descs['AM'] = 'Admin Message::Admin Messages'; this.descs['WC'] = 'Watched Critique::Watched Critiques'; this.messages = new Array(); this.req = new XMLHttpRequest(); this.loginReq = new XMLHttpRequest(); this.loginRetry = 0; this.timer = null; this.status = {}; this.status.message = "Started..."; this.status.error = false; this.panel = null; this.loadAnimText = new Array(); this.loadAnimText[0] = "Loading"; this.loadAnimText[1] = "Loading."; this.loadAnimText[2] = "Loading.."; this.loadAnimText[3] = "Loading..."; this.loadAnimStep = 0; this.didLoad = false; this.loadAnimTimer = null; this.alertUrl = null; this.firstTime = true; this.prev_deviations = new Array(); //0 - deviantName //1 - deviant link //2 - deviation name //3 - deviation link this.prev_notes = new Array(); //0 - deviant name //1 - deviant link //2 - item name //3 - item link this.prev_watch = new Array(); //0 - deviant name //1 - deviant link this.prev_comments = new Array(); //0 - deviant name //1 - deviant link //2 - deviation name //3 - deviation link //4 - comment link this.prev_favs = new Array(); //0 - deviant name //1 - deviant link //2 - deviation name //3 - deviation link //4 - collection name //5 - collection link this.prev_critiques = new Array(); //0 - critiquer name //1 - critiquer link //2 - critique link //3 - critiqued art link //4 - critiqued art name //5 - artist name //6 - artist link this.prev_journals = new Array(); //0 - deviantName //1 - deviant link //2 - journal name //3 - journal link } deviantAnywhere.prototype = { init: function() { panel = document.getElementById("dAPanel"); this.loadAnim(); lastMessages = ro_cvds_daUtils.getPref("lastMessages"); this.messages = ro_cvds_daUtils.JSON2array(lastMessages); if (ro_cvds_daUtils.getPref("customLook",ro_cvds_daUtils.boolPref)) { if (ro_cvds_daUtils.getPref("showIcon",ro_cvds_daUtils.boolPref)) panel.setAttribute("showIcon","true"); panel.setAttribute("class","customLook"); panel.style.backgroundColor = ro_cvds_daUtils.getPref("bkgcolor",ro_cvds_daUtils.stringPref); panel.style.borderColor = ro_cvds_daUtils.getPref("bkgcolor",ro_cvds_daUtils.stringPref); panel.style.color = ro_cvds_daUtils.getPref("textcolor",ro_cvds_daUtils.stringPref); } this.retrieveMessages(); }, loadAnim: function() { panel.setAttribute("label",this.loadAnimText[this.loadAnimStep]); if (this.loadAnimStep<this.loadAnimText.length-1) this.loadAnimStep++; else this.loadAnimStep = 0; if (!this.didLoad) loadAnimTimer = setTimeout("ro_cvds_daInstance.loadAnim()",500); }, killAnimation: function() { didLoad = true; clearTimeout(loadAnimTimer); }, retrieveMessages: function() { if (ro_cvds_daUtils.getPref("autoupdate",ro_cvds_daUtils.boolPref) || this.firstTime) { this.changeStatus("Retrieve messages in progress..."); this.firstTime = false; this.req.open('GET', this.messagesURL, true); this.req.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE; this.req.onreadystatechange = this.parseResponse; this.req.setRequestHeader("Pragma", "no-cache"); this.req.setRequestHeader("Cache-Control", "no-cache"); this.req.send(null); } this.timer = setTimeout("ro_cvds_daInstance.retrieveMessages()",ro_cvds_daUtils.getPref("checktime",ro_cvds_daUtils.intPref)*1000); }, changeStatus: function(message,wasError) { this.status.message = message; this.status.error = wasError; if (this.status.error) panel.setAttribute("mode","error"); else panel.setAttribute("mode","active"); }, parseResponse: function() { if (ro_cvds_daInstance.req.readyState == 4) { if(ro_cvds_daInstance.req.status == 200) { ro_cvds_daInstance.killAnimation(); html = this.responseText; //mesages labelText = "" overalText = "" match = ro_cvds_daInstance.REGEXP_MESSAGES.exec(html); if (match!=null) { labelText = match[1]; overalText = match[2]; } //deviations deviations = null; match = ro_cvds_daInstance.REGEXP_DEVIATIONS.exec(html); if (match != null && match.length > 1) deviations = match[1]; //notes notes = null; match = ro_cvds_daInstance.REGEXP_NOTES.exec(html); if (match != null && match.length > 1) notes = match[1]; //contests contests = null; match = ro_cvds_daInstance.REGEXP_CONTESTS.exec(html); if (match != null && match.length > 1) contests = match[1]; //admin admin = null; match = ro_cvds_daInstance.REGEXP_ADMIN.exec(html); if (match != null && match.length > 1) admin = match[1]; //critiques critiques = null; match = ro_cvds_daInstance.REGEXP_CRITIQUES.exec(html); if (match != null && match.length > 1) critiques = match[1]; if (overalText!=" No Messages") { _messages = new Array(); if (labelText != "") _messages = labelText.split(", "); match = ro_cvds_daInstance.REGEXP_DEVIANTNAME.exec(html); deviantName = match[1]+match[2]; newMessages = new Array(); // add notes if (notes!=null) newMessages['UN'] = notes; //add deviations if (deviations!=null) newMessages['D'] = deviations; //add contests if (contests!=null && contests!=0) newMessages['CA'] = contests; //add admin if (admin!=null && admin!=0) newMessages['AM'] = admin; //add critiques if (critiques!=null && critiques!=0) newMessages['WC'] = critiques; for (f=0;f<_messages.length;f++) { match = ro_cvds_daInstance.REGEXP_SPLIT_MSG.exec(_messages[f]); key = match[2]; value = match[1]; newMessages[key] = value; } ro_cvds_daInstance.handleMessages(newMessages, html); ro_cvds_daInstance.changeStatus("Messages successfully retrieved"); } else { ro_cvds_daInstance.killAnimation(); panel.setAttribute("label","No Messages"); ro_cvds_daUtils.setPref("lastMessages","",ro_cvds_daUtils.stringPref); ro_cvds_daInstance.messages = new Array(); ro_cvds_daInstance.changeStatus("Messages successfully retrieved: no messages"); } } //user not logged in ... DO NOT FORGET: to check if it's also the case of deviantart in read-only mode if (ro_cvds_daInstance.req.status == 403) { password = ro_cvds_daUtils.getUserData()?ro_cvds_daUtils.getUserData().password:null; if (ro_cvds_daUtils.getPref("useautologin",ro_cvds_daUtils.boolPref) && ro_cvds_daUtils.getPref("username",ro_cvds_daUtils.stringPref) && password) { ro_cvds_daInstance.login(password); } else { ro_cvds_daInstance.killAnimation(); panel.setAttribute("label","Not logged in"); ro_cvds_daInstance.changeStatus("You must be logged in on deviantArt or use the auto-login option from deviantAnywhere's advanced settings.",true); } } } }, setTooltip: function(message, title) { toolTip = document.getElementById("da-Tip"); while (toolTip.hasChildNodes()) toolTip.removeChild(toolTip.firstChild); if (title) { toolTipTitle = document.createElement("description"); toolTipTitle.setAttribute("value", title); toolTipTitle.setAttribute("id", "tipTitle"); toolTip.appendChild(toolTipTitle); } toolTipEntry = document.createElement("description"); toolTipEntry.setAttribute("value", message); toolTip.appendChild(toolTipEntry); }, handleMessages: function(newMessages, html) { hasNew = false; statusBarText = ""; toolTip = document.getElementById("da-Tip"); //clear the tooltip if (toolTip) while (toolTip.hasChildNodes()) toolTip.removeChild(toolTip.firstChild); if (toolTip) { toolTipEntry = document.createElement("description"); toolTipEntry.setAttribute("value", "Messages for "+deviantName); toolTipEntry.setAttribute("id", "tipTitle"); toolTip.appendChild(toolTipEntry); } //build the status bar message & tooltip hasNew = false; for (item in newMessages) { thisIsNew = false; if ((this.messages[item]==undefined && newMessages[item]!=0) || (this.messages[item] && this.messages[item]<newMessages[item])) { hasNew = true; thisIsNew = true; } this.messages[item] = newMessages[item]; if (this.messages[item]!=0) { statusBarText += this.messages[item]+item+" "; if (toolTip) { toolTipEntry = document.createElement("description"); toolTipEntry.setAttribute("value", this.messages[item]+" "+(this.descs[item]?this.descs[item].split("::")[this.messages[item]==1?0:1]:item)); toolTip.appendChild(toolTipEntry); } } } if (statusBarText=="") { statusBarText = "dA"; if (toolTip) { toolTipEntry = document.createElement("description"); toolTipEntry.setAttribute("value", "None"); toolTip.appendChild(toolTipEntry); } } panel.setAttribute("label",statusBarText); if (hasNew) { if (ro_cvds_daUtils.getPref("openNotification",ro_cvds_daUtils.boolPref)) this.getContents(html); if (ro_cvds_daUtils.getPref("playsound",ro_cvds_daUtils.boolPref)) this.playSound(ro_cvds_daUtils.getPref("sound")); if (ro_cvds_daUtils.getPref("openMsgOnNew",ro_cvds_daUtils.boolPref)) setTimeout("ro_cvds_daInstance.openURL(ro_cvds_daInstance.messagesURL,ro_cvds_daUtils.getPref('focusTab',ro_cvds_daUtils.boolPref))",500); ro_cvds_daUtils.setPref("lastMessages",ro_cvds_daUtils.array2JSON(newMessages),ro_cvds_daUtils.stringPref); } }, getContents: function(html) { this.prev_deviations = new Array(); this.prev_notes = new Array(); this.prev_watch = new Array(); this.prev_comments = new Array(); this.prev_favs = new Array(); deviations = this.preg_matchall(this.REGEXP_PREV_DEVIATIONS, html) for(f=0;f<deviations.length;f++) { this.prev_deviations[f] = new Array(); this.prev_deviations[f][0] = deviations[f][1]+deviations[f][3]; this.prev_deviations[f][1] = this.clearURL(deviations[f][2]); this.prev_deviations[f][2] = deviations[f][5]; this.prev_deviations[f][3] = this.clearURL(deviations[f][4]); } notes = this.preg_matchall(this.REGEXP_PREV_NOTES, html) for(f=0;f<notes.length;f++) { this.prev_notes[f] = new Array(); this.prev_notes[f][0] = notes[f][1]+notes[f][3]; this.prev_notes[f][1] = this.clearURL(notes[f][2]); this.prev_notes[f][2] = notes[f][5]; this.prev_notes[f][3] = this.clearURL(notes[f][4]); } favs = this.preg_matchall(this.REGEXP_PREV_FAVS, html) for(f=0;f<favs.length;f++) { this.prev_favs[f] = new Array(); this.prev_favs[f][0] = favs[f][1]+favs[f][3]; this.prev_favs[f][1] = this.clearURL(favs[f][2]); this.prev_favs[f][2] = favs[f][7]; this.prev_favs[f][3] = this.clearURL(favs[f][6]); this.prev_favs[f][4] = favs[f][5]; this.prev_favs[f][5] = this.clearURL(favs[f][4]); } watch = this.preg_matchall(this.REGEXP_PREV_WATCH, html) for(f=0;f<watch.length;f++) { this.prev_watch[f] = new Array(); this.prev_watch[f][0] = watch[f][1]+watch[f][3]; this.prev_watch[f][1] = this.clearURL(watch[f][2]); } comments = this.preg_matchall(this.REGEXP_PREV_COMMENTS, html) for(f=0;f<comments.length;f++) { this.prev_comments[f] = new Array(); this.prev_comments[f][0] = comments[f][1]+comments[f][3]; this.prev_comments[f][1] = this.clearURL(comments[f][2]); this.prev_comments[f][2] = comments[f][5]; this.prev_comments[f][3] = this.clearURL(comments[f][4]); this.prev_comments[f][4] = this.clearURL(comments[f][6]); } critiques = this.preg_matchall(this.REGEXP_PREV_CRITIQUES, html) for(f=0;f<critiques.length;f++) { this.prev_critiques[f] = new Array(); this.prev_critiques[f][0] = critiques[f][1]+critiques[f][3] this.prev_critiques[f][1] = this.clearURL(critiques[f][2]); this.prev_critiques[f][2] = this.clearURL(critiques[f][4]); this.prev_critiques[f][3] = this.clearURL(critiques[f][5]); this.prev_critiques[f][4] = critiques[f][6]; this.prev_critiques[f][5] = critiques[f][7]+critiques[f][9] this.prev_critiques[f][6] = this.clearURL(critiques[f][8]); } journals = this.preg_matchall(this.REGEXP_PREV_JOURNALS, html) for(f=0;f<journals.length;f++) { this.prev_journals[f] = new Array(); this.prev_journals[f][0] = journals[f][1]+journals[f][3]; this.prev_journals[f][1] = this.clearURL(journals[f][2]); this.prev_journals[f][2] = journals[f][5]; this.prev_journals[f][3] = this.clearURL(journals[f][4]); } this.showNotification(); }, clearURL: function(url) { return url.replace(/\\/g, ""); }, preg_matchall: function(regexp, target) { result = new Array(); hellFrezesOver = true; do{ parts = regexp.exec(target) if (parts==null) break; result[result.length] = parts; target = target.replace(regexp,""); }while (hellFrezesOver); return result; }, openURL: function(URL, alsoFocus) { if (alsoFocus) gBrowser.selectedTab = gBrowser.addTab(URL); else gBrowser.addTab(URL); }, openWindow: function(url,name,panel) { var windowMediator = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator); var topWindow = windowMediator.getMostRecentWindow(name); if (topWindow) topWindow.focus(); else { topWindow = windowMediator.getMostRecentWindow(null); topWindow.openDialog(url, "_blank", "chrome,toolbar,centerscreen", panel); } }, openConfig: function() { this.openWindow("chrome://devany/content/settings.xul","devAny:settings",["dA-main"]) }, openAbout: function() { this.openWindow("chrome://devany/content/about.xul","devAny:about",[]) }, playSound: function(userSound) { var sample = Components.classes["@mozilla.org/sound;1"].createInstance().QueryInterface(Components.interfaces.nsISound); var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); sample.play(ioService.newURI(userSound, null, null)); }, checkNow: function() { clearTimeout(this.timer); loginRetry = 0; this.retrieveMessages(); }, login: function(password) { var dataString = "username="+ro_cvds_daUtils.getPref("username",ro_cvds_daUtils.stringPref)+"&password="+password+"&reusetoken=1"; this.loginReq.open("POST", this.loginURL, true); this.loginReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); this.loginReq.onreadystatechange = this.loginEnd; this.loginReq.sendAsBinary(dataString); }, loginEnd: function() { if(this.readyState == 4 && this.status == 200) { if (ro_cvds_daInstance.loginRetry<3) { ro_cvds_daInstance.loginRetry++; clearTimeout(ro_cvds_daInstance.timer); ro_cvds_daInstance.retrieveMessages(); } else { ro_cvds_daInstance.killAnimation(); panel.setAttribute("label","Not logged in"); ro_cvds_daInstance.changeStatus("Invalid deviant name/password provided in auto-login",true); } } }, openStatus: function() { this.openWindow("chrome://devany/content/status.xul","devAny:status",[]) }, showNotification: function() { window.open("chrome://devany/content/alert.xul","notifier","chrome,dialog=yes,titlebar=no,popup=yes"); }, openMessages: function() { this.openURL(this.messagesURL,true); } } var ro_cvds_daInstance = new deviantAnywhere(); function ro_cvds_initDa() { ro_cvds_daInstance.init(); } window.addEventListener("load", ro_cvds_initDa, false);